package com.zfei.gfb.util;

import java.util.regex.Pattern;

/**
 * @author fgh
 */
public class RegexKit {

    private static final Pattern fixedPhonePattern = Pattern
            .compile("^(\\+?0*86-?)?(0+)?([0-9]{2,3})?([2-8][0-9]{6,7})([0-9]{1,4})?$");

    private static final Pattern domesticPhonePattern = Pattern
            .compile("^(0086[\\d-]+)|(\\+86[\\d-]+)|(0[1-9][\\d-]+)|([1-9][\\d-]+)$");

    private static final Pattern mobilePattern = Pattern
            .compile("^\\+\\d{1,4}-\\d+|1[3,4,5,6,7,8,9]\\d{9}");

    private static final Pattern domesticMobilePattern = Pattern
            .compile("^1[3,4,5,6,7,8,9]\\d{9}");

    private static final Pattern broadenDomesticMobilePattern = Pattern
            .compile("1[3,4,5,6,7,8,9](\\s*-?\\s*\\d){9}");

    private static final Pattern domesticMobileIllegalCharsPattern = Pattern
            .compile("\\s*-?\\s*");

    private static final Pattern internationalMobilePattern = Pattern
            .compile("^\\+\\d{1,4}-\\d+");

    private static final Pattern foreignerMobilePattern = Pattern
            .compile("^\\d{5,}$");

    private static final Pattern telecomMobilePattern = Pattern
            .compile("^((106|133|149|153|173|174|177|180|181|189|190|191|199)\\d{8})|((1700|1701|1702)\\d{7})");

    private static final Pattern unicomMobilePattern = Pattern
            .compile("^((130|131|132|145|155|156|166|171|175|176|185|186)\\d{8})|((1707|1708|1709)\\d{7})");

    private static final Pattern emailPattern = Pattern
            .compile("^[_A-Za-z0-9-+]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$");

    private static final Pattern mobileEmailPattern = Pattern
            .compile("^1[3,4,5,7,8]\\d{9}@([a-zA-Z0-9_-])+(\\.([a-zA-Z0-9_-])+)+");

    private static final Pattern yidPattern = Pattern
            .compile("[a-zA-Z][a-zA-Z0-9_]{5,19}"); // 易信号：以字母开头的字母+数字+下划线，6-20位

    private static final Pattern yidPatternForFindUser = Pattern
            .compile("[a-zA-Z][a-zA-Z0-9_]{4,19}"); // 易信号：以字母开头的字母+数字+下划线，5-20位

    private static final Pattern ecpidPattern = Pattern
            .compile(".+@ecplive\\.com");

    private static final String ipDigitPattern = "(25[0-5]|2[0-4]\\d|1\\d{2}|[1-9]?\\d)";

    private static final Pattern ipPattern = Pattern
            .compile(String.format("%s(\\.%s){3}", ipDigitPattern, ipDigitPattern));

    private static final Pattern internalIpPattern = Pattern
            .compile(String.format("(10(\\.%s){3})|(172\\.(1[6-9]|2\\d|3[01])(\\.%s){2})|(192\\.168(\\.%s){2})|(127\\.0\\.0\\.%s)",
                    ipDigitPattern, ipDigitPattern, ipDigitPattern, ipDigitPattern));

    private static final Pattern numbersPattern = Pattern.compile("\\d+");

    private static final Pattern uuidPattern = Pattern
            .compile("^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$");

    private static Pattern iphoneOsPattern = Pattern.compile("OSVersion: (.+?) Device: (.+)");

    private static Pattern nickPattern = Pattern.compile("^[\\u4e00-\\u9fa5A-Z0-9a-zéëêèàâäáùüûúìïîí._-]+$");

    public static Pattern urlPattern = Pattern.compile("(https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]");

    public static Pattern emojiPattern = Pattern.compile("(?:[\uD83C\uDF00-\uD83D\uDDFF]|[\uD83E\uDD00-\uD83E\uDDFF]|[\uD83D\uDE00-\uD83D\uDE4F]|[\uD83D\uDE80-\uD83D\uDEFF]|[\u2600-\u26FF]\uFE0F?|[\u2700-\u27BF]\uFE0F?|\u24C2\uFE0F?|[\uD83C\uDDE6-\uD83C\uDDFF]{1,2}|[\uD83C\uDD70\uD83C\uDD71\uD83C\uDD7E\uD83C\uDD7F\uD83C\uDD8E\uD83C\uDD91-\uD83C\uDD9A]\uFE0F?|[\u0023\u002A\u0030-\u0039]\uFE0F?\u20E3|[\u2194-\u2199\u21A9-\u21AA]\uFE0F?|[\u2B05-\u2B07\u2B1B\u2B1C\u2B50\u2B55]\uFE0F?|[\u2934\u2935]\uFE0F?|[\u3030\u303D]\uFE0F?|[\u3297\u3299]\uFE0F?|[\uD83C\uDE01\uD83C\uDE02\uD83C\uDE1A\uD83C\uDE2F\uD83C\uDE32-\uD83C\uDE3A\uD83C\uDE50\uD83C\uDE51]\uFE0F?|[\u203C\u2049]\uFE0F?|[\u25AA\u25AB\u25B6\u25C0\u25FB-\u25FE]\uFE0F?|[\u00A9\u00AE]\uFE0F?|[\u2122\u2139]\uFE0F?|\uD83C\uDC04\uFE0F?|\uD83C\uDCCF\uFE0F?|[\u231A\u231B\u2328\u23CF\u23E9-\u23F3\u23F8-\u23FA]\uFE0F?)");

    public static boolean isFixedPhone(String str) {
        return fixedPhonePattern.matcher(str).matches();
    }

    public static boolean isDomesticPhone(String str) {
        return domesticPhonePattern.matcher(str).matches();
    }

    public static boolean isMobile(String str) {
        return mobilePattern.matcher(str).matches();
    }

    public static boolean isDomesticMobile(String str) {
        return domesticMobilePattern.matcher(str).matches();
    }

    public static boolean isBroadenDomesticMobile(String str) {
        return broadenDomesticMobilePattern.matcher(str).matches();
    }

    public static String normalizeDomesticMobile(String str) {
        return domesticMobileIllegalCharsPattern.matcher(str).replaceAll("");
    }

    public static boolean isInternationalMobile(String str) {
        return internationalMobilePattern.matcher(str).matches();
    }

    public static boolean isForeignerMobile(String str) {
        return foreignerMobilePattern.matcher(str).matches();
    }

    public static boolean isTelecomMobile(String str) {
        return telecomMobilePattern.matcher(str).matches();
    }

    public static boolean isUnicomMobile(String str) {
        return unicomMobilePattern.matcher(str).matches();
    }

    public static boolean isEmail(String str) {
        return emailPattern.matcher(str).matches();
    }

    public static boolean isMobileEmail(String str) {
        return mobileEmailPattern.matcher(str).matches();
    }

    public static boolean isYid(String str) {
        return yidPattern.matcher(str).matches();
    }

    public static boolean isYidForFindUser(String str) {
        return yidPatternForFindUser.matcher(str).matches();
    }

    public static boolean isEcpid(String str) {
        return ecpidPattern.matcher(str).matches();
    }

    public static boolean isIp(String str) {
        return ipPattern.matcher(str).matches();
    }

    public static boolean isInternalIp(String str) {
        return internalIpPattern.matcher(str).matches();
    }

    public static boolean isNumbers(String str) {
        return numbersPattern.matcher(str).matches();
    }

    public static boolean isUUID(String str) {
        return uuidPattern.matcher(str).matches();
    }

    public static boolean isIphoneOs(String str) {
        return iphoneOsPattern.matcher(str).matches();
    }

    public static boolean isNick(String nick) {
        return nickPattern.matcher(nick).matches();
    }

    public static boolean isUrl(String url) {
        return urlPattern.matcher(url).matches();
    }
}
